import type { HttpResponse } from "@/types/request";
import type { PlayItem } from "@/types/song";
import type { TrackItem } from "@/types/song";
import type { SongLevel } from "@/types/song";
import { request } from "@/utils/request";

interface UserPlayListRequest extends HttpResponse {
  playlist: PlayItem[];
}

interface UserPlayListResponse extends HttpResponse {
  playlist: PlayItem;
}

interface SongListDetailsRequest extends HttpResponse {
  songs: TrackItem[];
}

interface CheckSongResponse extends HttpResponse {
  success: boolean;
  message: string;
}
interface SongUrlResponse extends HttpResponse {
  data: {
    url: string;
    id: number;
    time: number;
  }[];
}

// 获取用户歌单
export const getUserPlaylist = (uid: number): Promise<UserPlayListRequest> =>
  request<UserPlayListRequest>({
    url: "/user/playlist",
    data: {
      uid,
    },
  });

// 获取歌单详情
export const getPlaylistDetail = (id: number): Promise<UserPlayListResponse> =>
  request<UserPlayListResponse>({
    url: "/playlist/detail",
    data: {
      id,
    },
  });

// 获取歌单所有歌曲
export const getPlaylistSongs = (data: {
  offset: number;
  limit: number;
  id: number;
}): Promise<SongListDetailsRequest> =>
  request<SongListDetailsRequest>({
    url: "/playlist/track/all",
    data,
  });

// 获取歌曲详情
export const getSongDetail = (ids: string): Promise<HttpResponse> =>
  request<HttpResponse>({
    url: "/song/detail",
    data: {
      ids,
    },
  });

// 音乐是否可用
export const checkMusic = (id: number): Promise<CheckSongResponse> =>
  request<CheckSongResponse>({
    url: "/check/music",
    data: {
      id,
    },
  });

// 获取音乐 url
export const getMusicUrl = (data: {
  id: number;
  level: SongLevel;
}): Promise<SongUrlResponse> =>
  request<SongUrlResponse>({
    url: "/song/url",
    data,
  });

// 发送验证码
export const sendCaptcha = (
  phone: string,
  ctcode: string
): Promise<HttpResponse> =>
  request<HttpResponse>({
    url: "/captcha/sent",
    data: {
      phone,
      ctcode,
    },
  });
